package com.example.lizhixin.object_three_team.Base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


/**
 * 所在包名：com.suneee.ftx.weilian.fragment
 * 描述：BaseFragment基类
 * 作者：陈强
 * 创建时间：2018/2/8
 * 修改人：
 * 修改时间：
 * 修改描述：
 */

public abstract class BaseFragment extends Fragment {
    public Activity mContext;
    private View inflaterView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (inflaterView == null) {
            inflaterView = inflater.inflate(attachLayoutRes(), container, false);
        }
        ViewGroup parent = (ViewGroup) inflaterView.getParent();
        if (parent != null) {
            parent.removeView(inflaterView);
        }

        return inflaterView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    protected abstract void initData();

    /**
     * 绑定布局文件
     *
     * @return 布局文件ID
     */
    protected abstract int attachLayoutRes();

    /**
     * 初始化 view控件
     */
    protected abstract void initView();

    protected void startActivity(Class clazz, boolean isFinish) {
        startActivity(new Intent(mContext, clazz));
        if (isFinish) {
            mContext.finish();
        }
    }

    protected void showTost(String msg) {
        Toast.makeText(getActivity().getApplication(), msg, Toast.LENGTH_SHORT).show();
    }

}